package com.service;

import static com.def.GenericDef.E_XPUASGRP_ALREADY_EXIST;
import static com.def.GenericDef.E_XPUASGRP_NOT_FOUND_USER;
import static com.def.GenericDef.E_XPUASGRP_OTHER_ERROR;
import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.def.GenericDef.RETURN_MESG_OK;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import antlr.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dao.IBisUserDao;
import com.icim.service.ICIMBaseService;
import com.model.Bis_user;
import com.tx.Xpuasgrp.XpuasgrpI;
import com.tx.Xpuasgrp.XpuasgrpIA;
import com.tx.Xpuasgrp.XpuasgrpO;
import com.tx.Xpuasgrp.XpuasgrpOA;
import com.util.GUIDGenerator;
import com.util.MqOpe;
import com.util.Util;

@Scope("prototype")
@Service("XpuasgrpService")
public class XpuasgrpService extends ICIMBaseService{
	private static Logger logger = Logger.getLogger(XpinqshtService.class);
	private static String TX_NAME = "XPUASGRP";
	@Autowired
	private XpuasgrpI xpuasgrpI ;
	private XpuasgrpIA xpuasgrpIA;
	@Autowired
	private XpuasgrpO xpuasgrpO ;
	@Autowired
	private IBisUserDao bisUserDao;
	@Autowired
	private MqOpe mqOpe;
	
	private Timestamp gEvtTimestamp;
	
	public String subMainProc(String strInMsg){
		logger.info("InTrx is "+ strInMsg );
		String reply ="";
		gEvtTimestamp = Util.getCurrentTimestamp();
		try{
			xpuasgrpI = (XpuasgrpI) mqOpe.inStr2Obj(strInMsg,xpuasgrpI);
			xpuasgrpO.setTrx_id("XPUASGRP");
			xpuasgrpO.setType_id("O");
			String actionFlg = xpuasgrpI.getAction_flg();
			if(actionFlg.equals("Q")){
				getUserProc( );
			}else if( actionFlg.equals("A") ){
				AddUserProc( );
			}else if( actionFlg.equals("U") ){
				UpdateUserProc();
			}else if( actionFlg.equals("D")){
				DeleteUserProc( );
			}else if( actionFlg.equals("K")){
				UpdateUserKeyProc( );
			} 
			if( RETURN_CODE_OK.equals(xpuasgrpO.getRtn_code()) ){
				bisUserDao.flushCurSession();
			}else{
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
			
		}catch( Exception ex ){
			logger.error(Util.stackTraceToString(ex));
			xpuasgrpO.setRtn_code(RETURN_CODE_UNKNOWN);
			xpuasgrpO.setRtn_mesg(ex.toString());
			
		}finally{
			reply = mqOpe.obj2Str(xpuasgrpO);
			logger.info(reply);
		}
		return reply;
		
	}
	public void getUserProc( ){
		List<XpuasgrpOA> oaryList = new ArrayList<XpuasgrpOA>();
		try{
			long tbl_cnt = xpuasgrpI.getTbl_cnt();
			if( tbl_cnt == 1){
				XpuasgrpIA iary = xpuasgrpI.getIary().get(0);
				String user_id = iary.getUsr_id();
				Bis_user bis_user = bisUserDao.get(user_id);
				if( bis_user == null){
					xpuasgrpO.setRtn_code(E_XPUASGRP_NOT_FOUND_USER +"");
					xpuasgrpO.setRtn_mesg("用户代码 ["+iary.getUsr_id()+"]不存在!");
					return ;
				}else{
					XpuasgrpOA oary = new XpuasgrpOA();
					oary.setUsr_id(bis_user.getUsr_id());
					oary.setUsr_name(bis_user.getUsr_name());
					oary.setDept_id_fk(bis_user.getDept_id_fk());
					oary.setLock_flg(bis_user.getLock_flg());
					oary.setReg_timestamp(bis_user.getReg_timestamp());
					oary.setUsr_e_name(bis_user.getUsr_e_name());
					oary.setUsr_card_id(bis_user.getUsr_card_id());
					oary.setUsr_head_flg(bis_user.getUsr_head_flg());
					oary.setUsr_job(bis_user.getUsr_job());
					oary.setUsr_job_loc(bis_user.getUsr_job_loc());
					oary.setUsr_key(bis_user.getUsr_key());
					oary.setUsr_level(bis_user.getUsr_level());
					oary.setUsr_loc(bis_user.getUsr_loc());
					oary.setUsr_mail(bis_user.getUsr_mail());
					oary.setUsr_phs(bis_user.getUsr_phs());
					oary.setUsr_sms(bis_user.getUsr_sms());
					oary.setUsr_typ(bis_user.getUsr_typ());
					oaryList.add(oary);
				}
			}else{
				List<Bis_user> bisUserList = bisUserDao.loadAll(); 
				if( bisUserList != null){
					for(Bis_user bis_user : bisUserList ){
						XpuasgrpOA oary = new XpuasgrpOA();
						oary.setUsr_id(bis_user.getUsr_id());
						oary.setUsr_name(bis_user.getUsr_name());
						oaryList.add(oary);
					}
					
				}
			}
			
		}catch(Exception ex){
			logger.error(Util.stackTraceToString(ex));
			xpuasgrpO.setRtn_code(E_XPUASGRP_OTHER_ERROR +"");
			xpuasgrpO.setRtn_mesg(ex.getMessage());
			return ;
		}
		xpuasgrpO.setRtn_code( RETURN_CODE_OK );
		xpuasgrpO.setRtn_mesg( RETURN_MESG_OK );
		xpuasgrpO.setOary(oaryList);
		xpuasgrpO.setTbl_cnt(oaryList.size());
		
	}
	
	public void AddUserProc( ){
		long tbl_cnt = xpuasgrpI.getTbl_cnt();
		for(int i=0;i<tbl_cnt;i++){
			XpuasgrpIA iary = xpuasgrpI.getIary().get(i);
			String user_id = iary.getUsr_id();
			Bis_user bis_user = bisUserDao.get(user_id);
			if( bis_user != null ){
				xpuasgrpO.setRtn_code(E_XPUASGRP_ALREADY_EXIST +"");
				xpuasgrpO.setRtn_mesg(" 用户代码[" + iary.getUsr_id()+ " ]已经存在，无法添加!");
				return ;
			}
			bis_user = new Bis_user();
			bis_user.setUnq_seq_id(GUIDGenerator.javaGUID());
			bis_user.setUsr_id(iary.getUsr_id());
			bis_user.setDept_id_fk(iary.getDept_id_fk());
			bis_user.setLock_flg(iary.getLock_flg());
			bis_user.setUsr_e_name(iary.getUsr_e_name());
			bis_user.setUsr_head_flg(iary.getUsr_head_flg());
			bis_user.setUsr_job(iary.getUsr_job());
			bis_user.setUsr_job_loc(iary.getUsr_job_loc());
			bis_user.setUsr_level(iary.getUsr_level());
			bis_user.setUsr_loc(iary.getUsr_loc());
			bis_user.setUsr_mail(iary.getUsr_mail());
			bis_user.setUsr_name(iary.getUsr_name());
			bis_user.setUsr_phs(iary.getUsr_phs());
			bis_user.setUsr_typ(iary.getUsr_typ());
			bis_user.setUsr_sms(iary.getUsr_sms());
			bis_user.setReg_timestamp(iary.getReg_timestamp());
			bis_user.setEvt_timestamp(gEvtTimestamp);
			bis_user.setEvt_usr(iary.getEvt_usr());

			//默认密码123456
			if(org.apache.commons.lang.StringUtils.isEmpty(iary.getUsr_key())){
				bis_user.setUsr_key("123456");
			}

			bisUserDao.save(bis_user);
		}
		xpuasgrpO.setRtn_code( RETURN_CODE_OK );
		xpuasgrpO.setRtn_mesg( RETURN_MESG_OK );
	}
	public void UpdateUserProc( ){
		long tbl_cnt = xpuasgrpI.getTbl_cnt();
		for(int i=0;i<tbl_cnt;i++){
			XpuasgrpIA iary = xpuasgrpI.getIary().get(i);
			String user_id = iary.getUsr_id();
			Bis_user bis_user = bisUserDao.get(user_id);
			if( bis_user == null){
				xpuasgrpO.setRtn_code(E_XPUASGRP_NOT_FOUND_USER +"");
				xpuasgrpO.setRtn_mesg("该用户代码 ["+iary.getUsr_id()+"]不存在,无法更新!");
				return ;
			}
			bis_user.setDept_id_fk(iary.getDept_id_fk());
			bis_user.setLock_flg(iary.getLock_flg());
			bis_user.setUsr_e_name(iary.getUsr_e_name());
			bis_user.setUsr_head_flg(iary.getUsr_head_flg());
			bis_user.setUsr_job(iary.getUsr_job());
			bis_user.setUsr_job_loc(iary.getUsr_job_loc());
			bis_user.setUsr_level(iary.getUsr_level());
			bis_user.setUsr_loc(iary.getUsr_loc());
			bis_user.setUsr_mail(iary.getUsr_mail());
			bis_user.setUsr_name(iary.getUsr_name());
			bis_user.setUsr_phs(iary.getUsr_phs());
			bis_user.setUsr_typ(iary.getUsr_typ());
			bis_user.setUsr_sms(iary.getUsr_sms());
			bis_user.setReg_timestamp(iary.getReg_timestamp());
			bis_user.setEvt_timestamp(gEvtTimestamp);
			bis_user.setEvt_usr(iary.getEvt_usr());
			bisUserDao.update(bis_user);
		}
		xpuasgrpO.setRtn_code( RETURN_CODE_OK );
		xpuasgrpO.setRtn_mesg( RETURN_MESG_OK );
	}
	public void UpdateUserKeyProc(){
		long tbl_cnt = xpuasgrpI.getTbl_cnt();
		for(int i=0;i<tbl_cnt;i++){
			XpuasgrpIA iary = xpuasgrpI.getIary().get(i);
			String user_id = iary.getUsr_id();
			Bis_user bis_user = bisUserDao.get(user_id);
			if( bis_user == null){
				xpuasgrpO.setRtn_code(E_XPUASGRP_NOT_FOUND_USER +"");
				xpuasgrpO.setRtn_mesg("该用户代码 ["+iary.getUsr_id()+"]不存在,无法更新!");
				return ;
			}
			bis_user.setUsr_key(iary.getUsr_key());
			bis_user.setEvt_timestamp(gEvtTimestamp);
			bis_user.setEvt_usr(iary.getEvt_usr());
			bisUserDao.update(bis_user);
		}
		xpuasgrpO.setRtn_code( RETURN_CODE_OK );
		xpuasgrpO.setRtn_mesg( RETURN_MESG_OK );
	}
	public void DeleteUserProc( ){
		long tbl_cnt = xpuasgrpI.getTbl_cnt();
		for(int i=0;i<tbl_cnt;i++){
			XpuasgrpIA iary = xpuasgrpI.getIary().get(i);
			String user_id = iary.getUsr_id();
			Bis_user bis_user = bisUserDao.get(user_id);
			if( bis_user == null){
				xpuasgrpO.setRtn_code(E_XPUASGRP_NOT_FOUND_USER +"");
				xpuasgrpO.setRtn_mesg("用户代码 ["+iary.getUsr_id()+"]不存在,无法删除!");
				return ;
			}
			bisUserDao.delete(bis_user);
		}
		xpuasgrpO.setRtn_code( RETURN_CODE_OK );
		xpuasgrpO.setRtn_mesg( RETURN_MESG_OK );
	}
}
